home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / amitcp2_x_gcc.lha / gethostname.c < prev    next >
C/C++ Source or Header  |  1994-01-12  |  2KB  |  92 lines

  1. char RCS_ID_GETHOSTNAME_C[] = "$Id: gethostname.c,v 1.5 1994/01/12 18:35:20 jasegler Exp jasegler $";
  2. /*
  3.  * gethostname.c -- get host name from the environment variable "HOSTNAME"
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Tue Apr 27 17:49:15 1993 jraja
  12.  * Last modified: Fri Jun  4 01:58:48 1993 ppessi
  13.  *
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <errno.h>
  20.  
  21. /****** net.lib/gethostname ******************************************
  22.  
  23.  *   NAME
  24.  *       gethostname -- get the name of the host
  25.  *
  26.  *   SYNOPSIS
  27.  *       error = gethostname(name, namelen);
  28.  *
  29.  *       int gethostname(char *, int);
  30.  *
  31.  *   FUNCTION
  32.  *       Get the name of the host to the buffer name of length namelen.
  33.  *       The name is taken from the environment variable "HOSTNAME"
  34.  *       where it SHOULD reside.
  35.  *
  36.  *   INPUTS
  37.  *       name    - Pointer to the buffer where the name should be
  38.  *                 stored.
  39.  *       namelen - Length of the buffer name.
  40.  *
  41.  *   RESULT
  42.  *       error   - 0 on success, -1 in case of an error. The global
  43.  *                 variable errno will be set to indicate the error as
  44.  *                 follows:
  45.  *
  46.  *                 ENOENT - The environment variable "HOSTNAME" is not
  47.  *                          found.
  48.  *
  49.  *   EXAMPLE
  50.  *       char hostname[MAXHOSTNAMELEN];
  51.  *       int error;
  52.  *
  53.  *       error = gethostname(hostname, sizeof(hostname));
  54.  *       if (error < 0)
  55.  *         exit(10);
  56.  *
  57.  *       printf("My name is \"%s\".\n", hostname);
  58.  *
  59.  *   NOTES
  60.  *       This function is included for source compatibility with Unix
  61.  *       systems.
  62.  *       The ENOENT errno value is AmiTCP/IP addition.
  63.  *
  64.  *   BUGS
  65.  *       Unlike the Unix version, this version assures that the
  66.  *       resulting string is always NULL-terminated.
  67.  *
  68.  *   SEE ALSO
  69.  *       getenv()
  70.  *****************************************************************************
  71.  *
  72.  */
  73.  
  74. int
  75. gethostname (char *name, int namelen)
  76. {
  77.   char Buffer[80];
  78.   char *cp = getenv ("HOSTNAME");
  79.  
  80.   if (cp == NULL)
  81.     {
  82.       errno = ENOENT;
  83.       return -1;
  84.     }
  85.   /* used to use a function called stccpy.. didn't have it but I figured it
  86.      does something like this.. :) */
  87.   sprintf (Buffer, "%%%ds", namelen);
  88.   sprintf (name, Buffer, cp);
  89.   free (cp);
  90.   return 0;
  91. }
  92.